/* ==================================================================
* Copyright (c) 2024/11/15 SCNU LiuJunJie z-ken
* All rights reserved.
* fileName:	Tree.cpp
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the xxx Group. and
* its contributors.
* 4. Neither the name of the Group nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Author,GROUP AND CONTRIBUTORS
* ===================================================================
* Author: LiuJunJie z-ken.
*/

//
// Created by zken on 2024/11/6.
//

#include "Tree.h"

void Tree::showTreeNodes() {
    if (rootId == -1) {
        cout << "根节点错误，请检测程序允许情况" << endl;
        return;
    }
    showTreeNodesSupport(rootId, 0);
}

void Tree::showTreeNodesSupport(int rootId, int depth) {
    LalrTreeNode rootNode = this->treeNodes[rootId];
    string outputStr = rootNode.value;
    cout << string(depth, '.') << outputStr << endl;
    for (int i = 0; i < rootNode.childs.size(); i++) {
        int childId = rootNode.childs[i];
        showTreeNodesSupport(childId, depth + 1);
    }
}

int Tree::addNode(string value, vector<int> childs) {
    LalrTreeNode newNode;
    newNode.id = this->treeNodes.size();
    newNode.value = value;
    newNode.childs = childs;
    this->treeNodes.push_back(newNode);
    return newNode.id;
}
